/**
 * @author yzhou2
 */
WatchlistSummary=function(container,config,callback){
	var qslan=QS_Lang.watchlist;
	this.container=container;
	this.config={
		height:35
	};
	this.callback={};
	if(config){
		$.extend(this.config,config);
	}
	if(callback){
        $.extend(this.callback,callback);
    }
	this.html='<div class="rtq-wl-summary" style="height:' + this.config.height + 'px"><div class="rtq-wl-sum-mixcry">'+qslan["mixcurrency"]+'</div><div class="rtq-wl-sum-container clearfix">' +
        '<div class="rtq-wl-sum-r"><div class="rtq-wl-sum-rt"></div><div class="rtq-wl-sum-rb"></div></div>' +
        '<div class="rtq-wl-sum-l"><div class="rtq-wl-sum-lt">'+qslan.title+'<a class="rtq-wl-sum_refresh" href="javascript:void(0)">'+qslan.refresh+'</a></div><div class="rtq-wl-sum-lb"></div></div>' +
        '</div></div>';
    this.el=null;
	this.subEl=null;
	this.init();
};
WatchlistSummary.prototype={
	init:function(){
		this.el=$(this.html).appendTo(this.container);
		this.subEl={
            'lt': this.el.find('.rtq-wl-sum-lt'),
            'lb': this.el.find('.rtq-wl-sum-lb'),
            'rt': this.el.find('.rtq-wl-sum-rt'),
            'rb': this.el.find('.rtq-wl-sum-rb')			
		}
		this._initEvent();
        this.hide();
	},
	_initEvent:function(){
		var self=this;
		this.subEl['lt'].find('.rtq-wl-sum_refresh').bind('click.watchlistSummary',function(e){
			if(typeof self.callback.onClickRefresh=='function'){
				self.callback.onClickRefresh();
			}
            //self.calculateSummary(self.profile.currentId);
        });
	},
	show:function(id,visibleHoldIds,callback){
		this.el.show();
        this._emptySummary();
        this.calculateSummary(id,visibleHoldIds);
		if(typeof callback=='function'){
			callback();
		}
	},
	hide:function(callback){
		this.el.hide();
		if(typeof callback=='function'){
			callback();
		}
	},
	calculateSummary:function(id,visibleHoldIds){
		if (id == 0) {
            return;
        }
        var self = this,qslan=QS_Lang.watchlist;        
        //var holdIds = this.grid.getIDs();
        QS_watchlistDataManager.getPortfolioSummary(id, visibleHoldIds, function(isMix, isEmpty, o){
            if (isMix) {
                self.el.find('.rtq-wl-sum-mixcry').show();
                self.el.find('.rtq-wl-sum-container').hide();
            }
            else {
                self.el.find('.rtq-wl-sum-mixcry').hide();
                self.el.find('.rtq-wl-sum-container').show();
                
                if (isEmpty) {
                    //self.subEl['lt'][0].innerHTML='Summary for '+QS_watchlistDataManager.getNameById(id);
                    self.subEl['lb'][0].innerHTML = String.format(qslan["lastupdated"],'--');
                    self.subEl['rt'][0].innerHTML = String.format(qslan["tccontent"],'0.00','0.00','0.00%');
                    self.subEl['rb'][0].innerHTML = String.format(qslan["mvcontent"],'0.00','0.00','0.00%');
                }
                else {
                    if (o.portId == id) {
                        //self.subEl['lt'][0].innerHTML='Summary for '+QS_watchlistDataManager.getNameById(id);
                        var tc=o.TC||'0.00',pl=o.PL||'0.00',plp=o.PLP||'0.00',mv=o.MV||'0.00',dpl=o.DPL||'0.00',dplp=o.DPLP||'0.00';
                        self.subEl['lb'][0].innerHTML = String.format(qslan["lastupdated"],o.updatedDate||'--');
                        self.subEl['rt'][0].innerHTML = String.format(qslan["tccontent"],tc,pl,plp);
                        self.subEl['rb'][0].innerHTML = String.format(qslan["mvcontent"],mv,dpl,dplp);
                    }
                }
            }
        });
	},
	_emptySummary:function(){
		this.subEl['lb'][0].innerHTML ='';
        this.subEl['rt'][0].innerHTML ='';
        this.subEl['rb'][0].innerHTML ='';
        this.el.find('.rtq-wl-sum-mixcry').hide();
        this.el.find('.rtq-wl-sum-container').hide();
	},
	clear:function(){
		this.subEl['lt'].unbind('.watchlistSummary');
		for (var p in this) {
            if (!p || p == 'clear') {
                continue;
            }
            this[p] = null;
            delete this[p];
        }
	}
};
